package com.et.core.store

import android.content.ContentResolver
import android.content.ContentValues
import android.database.Cursor
import android.net.Uri
import android.util.Log
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith


private const val TAG = "RoomProviderTest"

@RunWith(AndroidJUnit4::class)
class RoomProviderTest {
    private var contentResolver: ContentResolver? = null

    @Before
    fun setUp() {
        Log.d(TAG, "setUp")
        DataBaseUtil.init(InstrumentationRegistry.getInstrumentation().context)
        DataBaseUtil.channelDao(DataBaseUtil.IFileBox<IChannelDao>().apply {
            this.dao?.insert(ChannelEntity.create(11, 1))
        })
        contentResolver = InstrumentationRegistry.getInstrumentation().context.contentResolver
    }

    @Test
    fun insert() {
        val insertUri: Uri? = contentResolver!!.insert(GOODS_URI, contentValues())
        Log.d(TAG, "insert uri = ${insertUri?.authority}")
    }

    @Test
    fun update() {
        val count = contentResolver!!.update(GOODS_URI, contentValues().apply {
            put("price", "123")
        }, null, null)
        Log.d(TAG, "update count =${count}")
    }

    private fun contentValues(): ContentValues {
        val contentValues = ContentValues()
        contentValues.put("id", "1101")
        contentValues.put("box", "11")
        contentValues.put("channel", "1")
        contentValues.put("label", "label")
        contentValues.put("price", "1")
        contentValues.put("status", "$ENABLED")
        contentValues.put("quantity", "1")
        contentValues.put("barcode", "1111")
        contentValues.put("name", "name")
        contentValues.put("image_path_local", "path_name")
        contentValues.put("discount", "100")
        return contentValues
    }

    @Test
    fun query() {
        Log.d(TAG, "start test query")
        val cursor = contentResolver!!.query(GOODS_URI, null, null, null, null)
        if (cursor!!.moveToFirst()) {
            do {
                Log.d(TAG, cursor2ChannelEntity(cursor).toString())
            } while (cursor.moveToNext())
        } else {
            Log.d(TAG, "nothing")
        }
        cursor.close()
    }

    @Test
    fun delete() {
        val uriGoodsID: Uri =
            Uri.parse("content://$AUTHORITY/$GOODS_TABLE_NAME/11001")
        val count = contentResolver!!.delete(uriGoodsID, null, null)
    }

    private fun cursor2ChannelEntity(cursor: Cursor): ChannelEntity {
        val box = cursor.getInt(cursor.getColumnIndex("box"))
        val channel = cursor.getInt(cursor.getColumnIndex("channel"))
        return ChannelEntity.create(box = box, channel = channel).apply {
            this.label = cursor.getString(cursor.getColumnIndex("label"))
            this.priceCent = cursor.getInt(cursor.getColumnIndex("price"))
            this.status = when (cursor.getInt(cursor.getColumnIndex("status"))) {
                0 -> ENABLED
                1 -> DISABLED
                2 -> LACK
                else -> DISABLED
            }
            this.quantity = cursor.getInt(cursor.getColumnIndex("quantity"))
            this.barcode = cursor.getString(cursor.getColumnIndex("barcode"))
            this.name = cursor.getString(cursor.getColumnIndex("name"))
            this.image = cursor.getString(cursor.getColumnIndex("image_path_local"))
            this.discount = cursor.getInt(cursor.getColumnIndex("discount"))
        }
    }

    @Test
    fun test() {
        insert()
        Log.d(TAG, "============================================================")
        query()
        Log.d(TAG, "============================================================")

        update()
        Log.d(TAG, "============================================================")
        query()
        Log.d(TAG, "============================================================")

        delete()
        Log.d(TAG, "============================================================")
        query()
        Log.d(TAG, "============================================================")

    }

    companion object {
        private val GOODS_URI: Uri =
            Uri.parse("content://$AUTHORITY/$GOODS_TABLE_NAME")
    }
}